package chess;

import java.awt.BorderLayout;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Image;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import java.awt.event.WindowAdapter;

//本人Gitee主页：https://gitee.com/mimocvb
public class ChessBoard extends JFrame {
	
	static Board board;
	
	Font f = new Font("微软雅黑",Font.BOLD,13);
	
	private static final long serialVersionUID = 1L;
	private JPanel downBar;
	private JButton restartbutton;
	private JButton backbutton;
	private JButton exitbutton;
	private JButton stylebutton;
	private JMenuItem restart;
	private JMenuItem back;
	private static int ab;
	private JMenuItem exit;
    public void windowClosed(WindowEvent event) {}
    static Image image4 = (Image) new ImageIcon("file/kuang.png").getImage();
	
	public ChessBoard ()
	{
		
		board = new Board();
		
		setTitle("普通双人对弈");
		
		MyItemListener lis = new MyItemListener();

		downBar = new JPanel();
		restartbutton = new JButton("重新开始");
		backbutton = new JButton("悔棋");
		stylebutton = new JButton("换肤");
		exitbutton = new JButton("退出");
		
		exitbutton.setFont(f);
		restartbutton.setFont(f);
		stylebutton.setFont(f);
		backbutton.setFont(f);
		
		stylebutton.addActionListener(lis);
		exitbutton.addActionListener(lis);
		backbutton.addActionListener(lis);
		restartbutton.addActionListener(lis);
		downBar.setLayout(new FlowLayout(FlowLayout.LEFT));
		downBar.add(restartbutton);
		downBar.add(backbutton);
		downBar.add(stylebutton);
		downBar.add(exitbutton); 
		
		setIconImage(new ImageIcon("file/icon.png").getImage());
		
        Toolkit tk = Toolkit.getDefaultToolkit();
        Dimension screen = tk.getScreenSize();
        
        int x = Math.max(0, (screen.width / 2 - getWidth()) / 2);
        int y = Math.max(0, (screen.height / 2 - getHeight()) / 2);
        setLocation(x, y);
		
		add(downBar, BorderLayout.SOUTH);
		add (board);
		setSize(739, 800);
		setVisible(false);
		
    	//鼠标样式
    	Toolkit kit=Toolkit.getDefaultToolkit();
    	Image img=kit.getImage("file/mouse.png");
    	Cursor mouse=kit.createCustomCursor(img, new Point(1,1), "stick");
    	setCursor(mouse);
		
		ab = 3;
		
		this.addWindowListener(new WindowAdapter() {
			 
			public void windowClosing(WindowEvent e) {

					ab = 3;
	    	
					dispose();
			}
			 
		});
		
		// 把主窗口放到屏幕中间
		center(this);
		
		//设置关闭按钮
		setDefaultCloseOperation( JFrame.DO_NOTHING_ON_CLOSE );
		
		//设置禁止调整窗口大小
		setResizable(false); 
	}
	

	private class MyItemListener implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			// TODO Auto-generated method stub
			Object obj = e.getSource();
			if (obj == restart || obj == restartbutton)
			{
				System.out.println("重新开始...");
				board.restartGame();
			}
			else if (obj == exit || obj == exitbutton)
			{

				dispose();
				
				ab=3;
				
			}
			else if (obj == back || obj == backbutton)
			{
				System.out.println("悔棋...");
				board.goback();
			}
			else if(obj == stylebutton){
				
				board.style();
				
			}
		}
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
	}
	
    // 把主窗口放到屏幕中间
    private void center(Window frame) {
        Toolkit tk = Toolkit.getDefaultToolkit();
        Dimension screen = tk.getScreenSize();
        
        int x = Math.max(0, (screen.width - frame.getWidth()) / 2);
        int y = Math.max(0, (screen.height - frame.getHeight()) / 2);
        frame.setLocation(x, y);
    }  

    public static void xianshi(){
    	
    	if(ab==2) {
    		
    		String s=String.format("请勿多开");
    		JOptionPane.showMessageDialog(null, s);
    	}
    	else{
    		
    		ChessBoard board1 = new ChessBoard();
    		
    		board.restartGame();
    		
    		board1.setVisible(true);
    		
    		ab = 2;
    		
    	}
    }
}
